/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T010Substance;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T011Interaction;
import java.util.ArrayList;
import java.util.Collection;
import ep.persistence.entities.T007Illness;

/**
 *
 * @author Spyros
 */
public class T010SubstanceJpaController {

    public T010SubstanceJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T010Substance t010Substance) {
        if (t010Substance.getT011InteractionCollection() == null) {
            t010Substance.setT011InteractionCollection(new ArrayList<T011Interaction>());
        }
        if (t010Substance.getT011InteractionCollection1() == null) {
            t010Substance.setT011InteractionCollection1(new ArrayList<T011Interaction>());
        }
        if (t010Substance.getT007Illnesses() == null) {
            t010Substance.setT007Illnesses(new ArrayList<T007Illness>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<T011Interaction> attachedT011InteractionCollection = new ArrayList<T011Interaction>();
            for (T011Interaction t011InteractionCollectionT011InteractionToAttach : t010Substance.getT011InteractionCollection()) {
                t011InteractionCollectionT011InteractionToAttach = em.getReference(t011InteractionCollectionT011InteractionToAttach.getClass(), t011InteractionCollectionT011InteractionToAttach.getId());
                attachedT011InteractionCollection.add(t011InteractionCollectionT011InteractionToAttach);
            }
            t010Substance.setT011InteractionCollection(attachedT011InteractionCollection);
            Collection<T011Interaction> attachedT011InteractionCollection1 = new ArrayList<T011Interaction>();
            for (T011Interaction t011InteractionCollection1T011InteractionToAttach : t010Substance.getT011InteractionCollection1()) {
                t011InteractionCollection1T011InteractionToAttach = em.getReference(t011InteractionCollection1T011InteractionToAttach.getClass(), t011InteractionCollection1T011InteractionToAttach.getId());
                attachedT011InteractionCollection1.add(t011InteractionCollection1T011InteractionToAttach);
            }
            t010Substance.setT011InteractionCollection1(attachedT011InteractionCollection1);
            Collection<T007Illness> attachedT007Illnesses = new ArrayList<T007Illness>();
            for (T007Illness t007IllnessesT007IllnessToAttach : t010Substance.getT007Illnesses()) {
                t007IllnessesT007IllnessToAttach = em.getReference(t007IllnessesT007IllnessToAttach.getClass(), t007IllnessesT007IllnessToAttach.getId());
                attachedT007Illnesses.add(t007IllnessesT007IllnessToAttach);
            }
            t010Substance.setT007Illnesses(attachedT007Illnesses);
            em.persist(t010Substance);
            for (T011Interaction t011InteractionCollectionT011Interaction : t010Substance.getT011InteractionCollection()) {
                T010Substance oldT010SubstanceOfT011InteractionCollectionT011Interaction = t011InteractionCollectionT011Interaction.getT010Substance();
                t011InteractionCollectionT011Interaction.setT010Substance(t010Substance);
                t011InteractionCollectionT011Interaction = em.merge(t011InteractionCollectionT011Interaction);
                if (oldT010SubstanceOfT011InteractionCollectionT011Interaction != null) {
                    oldT010SubstanceOfT011InteractionCollectionT011Interaction.getT011InteractionCollection().remove(t011InteractionCollectionT011Interaction);
                    oldT010SubstanceOfT011InteractionCollectionT011Interaction = em.merge(oldT010SubstanceOfT011InteractionCollectionT011Interaction);
                }
            }
            for (T011Interaction t011InteractionCollection1T011Interaction : t010Substance.getT011InteractionCollection1()) {
                T010Substance oldT010Substance1OfT011InteractionCollection1T011Interaction = t011InteractionCollection1T011Interaction.getT010Substance1();
                t011InteractionCollection1T011Interaction.setT010Substance1(t010Substance);
                t011InteractionCollection1T011Interaction = em.merge(t011InteractionCollection1T011Interaction);
                if (oldT010Substance1OfT011InteractionCollection1T011Interaction != null) {
                    oldT010Substance1OfT011InteractionCollection1T011Interaction.getT011InteractionCollection1().remove(t011InteractionCollection1T011Interaction);
                    oldT010Substance1OfT011InteractionCollection1T011Interaction = em.merge(oldT010Substance1OfT011InteractionCollection1T011Interaction);
                }
            }
            for (T007Illness t007IllnessesT007Illness : t010Substance.getT007Illnesses()) {
                t007IllnessesT007Illness.getT010Substances().add(t010Substance);
                t007IllnessesT007Illness = em.merge(t007IllnessesT007Illness);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T010Substance t010Substance) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T010Substance persistentT010Substance = em.find(T010Substance.class, t010Substance.getId());
            Collection<T011Interaction> t011InteractionCollectionOld = persistentT010Substance.getT011InteractionCollection();
            Collection<T011Interaction> t011InteractionCollectionNew = t010Substance.getT011InteractionCollection();
            Collection<T011Interaction> t011InteractionCollection1Old = persistentT010Substance.getT011InteractionCollection1();
            Collection<T011Interaction> t011InteractionCollection1New = t010Substance.getT011InteractionCollection1();
            Collection<T007Illness> t007IllnessesOld = persistentT010Substance.getT007Illnesses();
            Collection<T007Illness> t007IllnessesNew = t010Substance.getT007Illnesses();
            List<String> illegalOrphanMessages = null;
            for (T011Interaction t011InteractionCollectionOldT011Interaction : t011InteractionCollectionOld) {
                if (!t011InteractionCollectionNew.contains(t011InteractionCollectionOldT011Interaction)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T011Interaction " + t011InteractionCollectionOldT011Interaction + " since its t010Substance field is not nullable.");
                }
            }
            for (T011Interaction t011InteractionCollection1OldT011Interaction : t011InteractionCollection1Old) {
                if (!t011InteractionCollection1New.contains(t011InteractionCollection1OldT011Interaction)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T011Interaction " + t011InteractionCollection1OldT011Interaction + " since its t010Substance1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T011Interaction> attachedT011InteractionCollectionNew = new ArrayList<T011Interaction>();
            for (T011Interaction t011InteractionCollectionNewT011InteractionToAttach : t011InteractionCollectionNew) {
                t011InteractionCollectionNewT011InteractionToAttach = em.getReference(t011InteractionCollectionNewT011InteractionToAttach.getClass(), t011InteractionCollectionNewT011InteractionToAttach.getId());
                attachedT011InteractionCollectionNew.add(t011InteractionCollectionNewT011InteractionToAttach);
            }
            t011InteractionCollectionNew = attachedT011InteractionCollectionNew;
            t010Substance.setT011InteractionCollection(t011InteractionCollectionNew);
            Collection<T011Interaction> attachedT011InteractionCollection1New = new ArrayList<T011Interaction>();
            for (T011Interaction t011InteractionCollection1NewT011InteractionToAttach : t011InteractionCollection1New) {
                t011InteractionCollection1NewT011InteractionToAttach = em.getReference(t011InteractionCollection1NewT011InteractionToAttach.getClass(), t011InteractionCollection1NewT011InteractionToAttach.getId());
                attachedT011InteractionCollection1New.add(t011InteractionCollection1NewT011InteractionToAttach);
            }
            t011InteractionCollection1New = attachedT011InteractionCollection1New;
            t010Substance.setT011InteractionCollection1(t011InteractionCollection1New);
            Collection<T007Illness> attachedT007IllnessesNew = new ArrayList<T007Illness>();
            for (T007Illness t007IllnessesNewT007IllnessToAttach : t007IllnessesNew) {
                t007IllnessesNewT007IllnessToAttach = em.getReference(t007IllnessesNewT007IllnessToAttach.getClass(), t007IllnessesNewT007IllnessToAttach.getId());
                attachedT007IllnessesNew.add(t007IllnessesNewT007IllnessToAttach);
            }
            t007IllnessesNew = attachedT007IllnessesNew;
            t010Substance.setT007Illnesses(t007IllnessesNew);
            t010Substance = em.merge(t010Substance);
            for (T011Interaction t011InteractionCollectionNewT011Interaction : t011InteractionCollectionNew) {
                if (!t011InteractionCollectionOld.contains(t011InteractionCollectionNewT011Interaction)) {
                    T010Substance oldT010SubstanceOfT011InteractionCollectionNewT011Interaction = t011InteractionCollectionNewT011Interaction.getT010Substance();
                    t011InteractionCollectionNewT011Interaction.setT010Substance(t010Substance);
                    t011InteractionCollectionNewT011Interaction = em.merge(t011InteractionCollectionNewT011Interaction);
                    if (oldT010SubstanceOfT011InteractionCollectionNewT011Interaction != null && !oldT010SubstanceOfT011InteractionCollectionNewT011Interaction.equals(t010Substance)) {
                        oldT010SubstanceOfT011InteractionCollectionNewT011Interaction.getT011InteractionCollection().remove(t011InteractionCollectionNewT011Interaction);
                        oldT010SubstanceOfT011InteractionCollectionNewT011Interaction = em.merge(oldT010SubstanceOfT011InteractionCollectionNewT011Interaction);
                    }
                }
            }
            for (T011Interaction t011InteractionCollection1NewT011Interaction : t011InteractionCollection1New) {
                if (!t011InteractionCollection1Old.contains(t011InteractionCollection1NewT011Interaction)) {
                    T010Substance oldT010Substance1OfT011InteractionCollection1NewT011Interaction = t011InteractionCollection1NewT011Interaction.getT010Substance1();
                    t011InteractionCollection1NewT011Interaction.setT010Substance1(t010Substance);
                    t011InteractionCollection1NewT011Interaction = em.merge(t011InteractionCollection1NewT011Interaction);
                    if (oldT010Substance1OfT011InteractionCollection1NewT011Interaction != null && !oldT010Substance1OfT011InteractionCollection1NewT011Interaction.equals(t010Substance)) {
                        oldT010Substance1OfT011InteractionCollection1NewT011Interaction.getT011InteractionCollection1().remove(t011InteractionCollection1NewT011Interaction);
                        oldT010Substance1OfT011InteractionCollection1NewT011Interaction = em.merge(oldT010Substance1OfT011InteractionCollection1NewT011Interaction);
                    }
                }
            }
            for (T007Illness t007IllnessesOldT007Illness : t007IllnessesOld) {
                if (!t007IllnessesNew.contains(t007IllnessesOldT007Illness)) {
                    t007IllnessesOldT007Illness.getT010Substances().remove(t010Substance);
                    t007IllnessesOldT007Illness = em.merge(t007IllnessesOldT007Illness);
                }
            }
            for (T007Illness t007IllnessesNewT007Illness : t007IllnessesNew) {
                if (!t007IllnessesOld.contains(t007IllnessesNewT007Illness)) {
                    t007IllnessesNewT007Illness.getT010Substances().add(t010Substance);
                    t007IllnessesNewT007Illness = em.merge(t007IllnessesNewT007Illness);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t010Substance.getId();
                if (findT010Substance(id) == null) {
                    throw new NonexistentEntityException("The t010Substance with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T010Substance t010Substance;
            try {
                t010Substance = em.getReference(T010Substance.class, id);
                t010Substance.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t010Substance with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<T011Interaction> t011InteractionCollectionOrphanCheck = t010Substance.getT011InteractionCollection();
            for (T011Interaction t011InteractionCollectionOrphanCheckT011Interaction : t011InteractionCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T010Substance (" + t010Substance + ") cannot be destroyed since the T011Interaction " + t011InteractionCollectionOrphanCheckT011Interaction + " in its t011InteractionCollection field has a non-nullable t010Substance field.");
            }
            Collection<T011Interaction> t011InteractionCollection1OrphanCheck = t010Substance.getT011InteractionCollection1();
            for (T011Interaction t011InteractionCollection1OrphanCheckT011Interaction : t011InteractionCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T010Substance (" + t010Substance + ") cannot be destroyed since the T011Interaction " + t011InteractionCollection1OrphanCheckT011Interaction + " in its t011InteractionCollection1 field has a non-nullable t010Substance1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T007Illness> t007Illnesses = t010Substance.getT007Illnesses();
            for (T007Illness t007IllnessesT007Illness : t007Illnesses) {
                t007IllnessesT007Illness.getT010Substances().remove(t010Substance);
                t007IllnessesT007Illness = em.merge(t007IllnessesT007Illness);
            }
            em.remove(t010Substance);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T010Substance> findT010SubstanceEntities() {
        return findT010SubstanceEntities(true, -1, -1);
    }

    public List<T010Substance> findT010SubstanceEntities(int maxResults, int firstResult) {
        return findT010SubstanceEntities(false, maxResults, firstResult);
    }

    private List<T010Substance> findT010SubstanceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T010Substance.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T010Substance findT010Substance(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T010Substance.class, id);
        } finally {
            em.close();
        }
    }

    public int getT010SubstanceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T010Substance> rt = cq.from(T010Substance.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
